<?php
namespace Admin\Controller;

class WechatUserController extends AdminController
{
    protected $userinfoModel;
    public function __construct() {
        parent::__construct();
        $this->userinfoModel = M("Userinfo",'tp_','DB_WX');
    }

    public function authenticationList(){
////
//        $dd = D('Community')->select();
//        foreach ($dd as &$v) {
//            $v['url'] = 0;
////            myDump($v);
//            D('Community')->data($v)->save();
//        }
//        die;
        $data = D('WxuserInfo')->authenticationList(20);
        $communityData = M('Community')->where(array('children_count'=>array('neq',0)))->select();
        $this->assign(array(
            'data' =>$data['data'],
            'page' =>$data['page'],
            'communityData' =>$communityData,
        ));
        $this->display();
    }

    public function edit(){
        $id = I('id');
        $data = D('WxuserInfo')->find($id);
        $this->assign('data',$data);
        $this->display();
    }

    public function editing(){
        $id = I('id');
        $room_id = I('room_id');
        $type = I('type');
        $community_id = I('community_id');
        $infoData = D('WxuserInfo')->find($id);
        $roomData = M('Room')->find($room_id);
        $infoData['type'] = $type;
        $infoData['community_id'] = $community_id;
        $infoData['address'] = $roomData['full_parent_name'].$roomData['name'];
        D('WxuserInfo')->data($infoData)->save();

        $userData= D('Wxuser')->find($infoData['customer_id']);
        $userData['community_id'] = $community_id;
        $userData['unit_id'] = I('unit_id');
        $userData['zone_id'] = I('zone_id');
        $userData['building_id'] = I('building_id');
        $userData['room_id'] = $room_id;
        D('Wxuser')->data($userData)->save();

        if ($room_id){
            $data['customer_id'] = $userData['id'];
            $data['room_id'] = $room_id;
            // $data['name'] = $info['realname'];
            // $data['tel'] = $info['tel'];
            // $data['sex'] = $info['sex'];
            $url = "http://zhcrm.wx91go.com/index.php?m=Api&c=Data&a=customerAuthentication";
            $ret = apiPost($url,$data);
            $crminfo = json_decode($ret,true);

            $crmid = $crminfo['crm_id'];
            M('QtwyCustomer', 'tp_', 'DB_WX')->where(array('id' => $infoData['customer_id']))->setField('crm_id',$crmid);
        }
        if($crmid){
            $res['status'] = 1;
            $res['msg'] = '修改成功！';
        }else{
            $res['status'] = false;
            $res['msg'] = '修改失败！';
        }
        $this->ajaxReturn($res);
    }

    public function peopleVerify(){
        $id = I('id');
        $isverify = I('isverify');
        if(!$id && !$isverify){
            $this->error('操作失败,缺少字段！');
        }
//        $this->success('操作成功！');
//        die;
        $ok = D('WxuserInfo')->people_audit($id,$isverify);
        if($ok)
            $this->success('操作成功！');
        else
            $this->error('操作失败！');
    }


    public function roll(){
        $_data = D('WxuserInfo')->roll();
        if($_data){
            $data['status'] = 1;
            $data['data'] = $_data;
        }else{
            $data['status'] = false;
        }
        $this->ajaxReturn($data);
    }

    public function dumpExcel(){
        $data = D('WxuserInfo')->dumpExcel();
//        myDump($data);
    }

}